package com.bilibili.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.dto.VideoAndUser;
import com.bilibili.service.SelectVideoService;
import com.bilibili.service.impl.SelectVideoServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

@WebServlet("/selectVideo.do")
public class SelectVideoServlet extends HttpServlet {
	private SelectVideoService sv= new SelectVideoServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper om=new ObjectMapper();
		List<VideoAndUser> videoList=sv.getAllVideos();
		if(request.getParameter("action") != null && request.getParameter("action").equals("send")) {
			response.setContentType("application/json");
			response.getWriter().write("{\"status\":\"success\"}");
			return;
		}
		if(request.getParameter("action") != null && request.getParameter("action").equals("get")) {
			String videoId=request.getParameter("videoId");
			int videoid=Integer.parseInt(videoId);
			VideoAndUser selectVideo=sv.getVideoByVideoId(videoid);
			String svStr=om.writeValueAsString(selectVideo);
			response.getWriter().write(svStr);
			return;
		}
		String videoStr=om.writeValueAsString(videoList);
		response.getWriter().write(videoStr);
	}

}
